from re import findall
from flask import Blueprint
from flask import json,jsonify,request
from db import db1 
bp_couer = Blueprint("coupon",__name__)

#接口
@bp_couer.route('/coupon',methods=['POST',"GET",'PUT','DELETE'])
def coupon():
    #添加
    if request.method == "POST":
        #获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        #构造sql语句
        sql = "insert into coupon values(5,'%s',%d,'%s','%s')"%(data['name'],int(data['total']),data['stime'],data['etime'])
        #pymysql
        db1.insert(sql)
        #返回结果
        return '添加成功'
    elif request.method=="GET":
         id = request.args.get('id')
         sql = "select * from coupon where id=%d"%(int(id))
         db1.find(sql)
         return jsonify({"code":200})
    elif request.method =="PUT":
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "update coupon set name='%s',total=%d,starttime='%s',endtime='%s' where id=%d"%(data['name'],int(data['total']),data['stime'],data['etime'],int(data['id']))
        db1.insert(sql)
        return jsonify({"code":200})
    elif request.method=="DELETE":
        id = request.args.get('id')
        sql = "delete from coupon where id=%d"%(int(id))
        return jsonify({"code":200})
        




           

